package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateUtil;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TActivity;
import com.bjpowernode.entity.TActivityDO;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.query.BaseQuery;
import com.bjpowernode.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Override
    public PageInfo<TActivity> getActivityPage(Integer current, ActivityQuery activityQuery) {
        //1.设置PageHelper (第一个参数是查第几页，第二个参数每页查多少条数据)
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        //2.查询数据库表的数据
        List<TActivity> list = tActivityMapper.selectActivityPage(activityQuery);

        //3.封装分页数据到PageInfo
        PageInfo<TActivity> info = new PageInfo<>(list);

        //4.返回PageInfo对象
        return info;
    }

    @Override
    public TActivity getActivityById(Integer id) {
        return tActivityMapper.selectById(id);
    }

    @Override
    public int saveActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        //对象属性值的复制，把activityQuery对象里面的属性值复制到tActivity对象的属性中
        //复制的条件：源对象 和 目标对象 里面的属性字段名要相同，属性的字段类型也相同，才可以复制
        BeanUtils.copyProperties(activityQuery, tActivity);
        tActivity.setStartTime(DateUtil.parse(activityQuery.getStartTime()).toJdkDate()); //hutool提供的日期处理类
        tActivity.setEndTime(DateUtil.parse(activityQuery.getEndTime()).toJdkDate());//hutool提供的日期处理类
        //创建时间
        tActivity.setCreateTime(new Date());
        //创建人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tActivity.setCreateBy(currentLoginUserId);
        return tActivityMapper.insertSelective(tActivity);
    }

    @Override
    public int updateActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        //对象属性值的复制，把activityQuery对象里面的属性值复制到tActivity对象的属性中
        //复制的条件：源对象 和 目标对象 里面的属性字段名要相同，属性的字段类型也相同，才可以复制
        BeanUtils.copyProperties(activityQuery, tActivity);
        tActivity.setStartTime(DateUtil.parse(activityQuery.getStartTime()).toJdkDate()); //hutool提供的日期处理类
        tActivity.setEndTime(DateUtil.parse(activityQuery.getEndTime()).toJdkDate());//hutool提供的日期处理类
        //编辑时间
        tActivity.setEditTime(new Date());
        //编辑人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tActivity.setEditBy(currentLoginUserId);
        return tActivityMapper.updateByPrimaryKeySelective(tActivity);
    }

    @Override
    public int delActivityById(Integer activityId) {
        return tActivityMapper.deleteByPrimaryKey(activityId);
    }

    @Override
    public int batchDelActivityByIds(List<String> idList) {
        return tActivityMapper.deleteByIdList(idList);
    }

    @Override
    public List<TActivityDO> getOngoingActivity() {
        return tActivityMapper.selectOngoingActivity();
    }
}
